package org.zn.note.java8;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * 工厂模式
 */
public class Ch08_Pattern_Factory {

    public static void main(String[] args) {
        Product p1 = ProductFactory.createProduct("loan");
        // lombok
        Supplier<Product> loanSupplier = Loan::new;
        Product p2 = loanSupplier.get();
        Map<String, Supplier<Product>> map = new HashMap<>();
        map.put("loan", Loan::new);
        map.put("stock", Stock::new);
        map.put("bond", Bond::new);

    }

    private static class ProductFactory {
        public static Product createProduct(String name) {
            switch (name) {
                case "loan":
                    return new Loan();
                case "stock":
                    return new Stock();
                case "bond":
                    return new Bond();
                default:
                    throw new RuntimeException("No such product " + name);
            }
        }
    }

    // 金融产品
    private interface Product {
    }

    // 贷款
    private static class Loan implements Product {
    }

    // 股票
    private static class Stock implements Product {
    }

    // 债券
    private static class Bond implements Product {
    }
}
